package com.ant.storage.service.impl;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ConverterUtils;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.ant.storage.dto.InventoryExcelDto;
import com.ant.storage.dto.ProcurementExcelDto;
import com.ant.storage.pojo.Commodity;
import com.ant.storage.pojo.Inventory;
import com.ant.storage.pojo.PlannedProcurement;
import com.ant.storage.service.IInventoryService;
import com.ant.storage.service.IPlannedProcurementService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author hzd
 * @version 1.0
 * @date 2023/9/14 11:35
 */
@Slf4j
@Service
public class ProcurementServiceExper implements ReadListener<ProcurementExcelDto> {

    @Autowired
    private IPlannedProcurementService procurementService;

    @Autowired
    private IInventoryService inventoryService;

    private List<ProcurementExcelDto> goodsExcelDTOList = ListUtils.newArrayList();
    private final List<PlannedProcurement> procurementList = ListUtils.newArrayList();
    private int count = 0;

    public ProcurementServiceExper() {
    }

    public ProcurementServiceExper(IPlannedProcurementService procurementService) {
        this.procurementService = procurementService;
    }

    // 2. 第二解析内容
    @Override
    public void invoke(ProcurementExcelDto dto, AnalysisContext analysisContext) {
        log.info("解析到第 {} 条数据:{}", (++count), JSON.toJSONString(dto));
        goodsExcelDTOList.add(dto);
    }

    // 1. 第一先读表头
    @Override
    public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
        Map<Integer, String> stringMap = ConverterUtils.convertToStringMap(headMap, context);
        Set<Integer> keySet = stringMap.keySet();
        System.out.println("该Excel表头信息是：");
        for (int i = 0; i < keySet.size(); i++) {
            System.out.println("第 " + (i + 1) + " 列 = " + stringMap.get(i));
        }
    }

    // 4. 最后调用
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("该Excel的所有数据解析完成！！！");
        goodsExcelDTOList.forEach(item -> {
            try {
                Commodity one = inventoryService.getOneCommodityId(item.getNumber());
                PlannedProcurement procurement = new PlannedProcurement();
                procurement.setCommodityId(one.getCommodityId());
                procurement.setCommodityName(one.getCommodityName());
                procurement.setCommodityType(one.getCommodityType());
                procurement.setInventoryBalance(item.getInventoryBalance());
                procurement.setPlannedInventory(item.getPlannedInventory());
                procurement.setUnitPrice(new BigDecimal(item.getUnitPrice()));
                procurement.setPurchaseAmount(item.getPurchaseAmount());
                procurementList.add(procurement);
            } catch (ParseException e) {
                log.error(e.getMessage());
            }
        });

        goodsExcelDTOList = ListUtils.newArrayList();
        procurementList.stream().forEach(item -> {
            //判断是否存在库存
            //不存新增
            item.setCreationTime(new Date());
            item.setPlannedNumber("202309");
            procurementService.save(item);
        });
    }
}
